/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package gui;

/**
 *
 * @author Runner
 */

import client.Requests;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.BoxLayout;
import java.awt.FlowLayout;
import javax.swing.SwingConstants;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Rectangle;
import javax.swing.DefaultComboBoxModel;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Insets;
import client.Member;
import java.util.List;
import javax.swing.ImageIcon;

public class AddTask {

    public JFrame f;
    private JButton jButton1;
    private JComboBox jComboBox1;
    private JLabel jLabelMember;
    private JLabel jLabelTask;
    private JPanel jPanel1;
    private JPanel jPanel2;
    private JPanel jPanel3;
    private JTextField jTextField1;
    public Requests req;

    private Integer[] memberIds;

    public AddTask(Requests req) throws Exception {

	this.req = req;
        f = new JFrame("Collab - Add Task");
        jPanel1 = new JPanel();
        jLabelMember = new JLabel();
        jComboBox1 = new JComboBox();
        jPanel2 = new JPanel();
        jLabelTask = new JLabel();
        jTextField1 = new JTextField();
        jPanel3 = new JPanel();
        jButton1 = new JButton();

        f.setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
        f.setBounds(new Rectangle(450, 250, 380, 120));
        f.setResizable(false);
        f.getContentPane().setLayout(new BoxLayout(f.getContentPane(), BoxLayout.Y_AXIS));

        jPanel1.setLayout(new FlowLayout(FlowLayout.LEFT));

        jLabelMember.setHorizontalAlignment(SwingConstants.RIGHT);
        jLabelMember.setText("Member:");
        jLabelMember.setMaximumSize(new Dimension(50, 20));
        jLabelMember.setMinimumSize(new Dimension(50, 20));
        jLabelMember.setPreferredSize(new Dimension(50, 20));
        jPanel1.add(jLabelMember);

        jComboBox1.setModel(new DefaultComboBoxModel(getMembers()));
        jComboBox1.setMaximumSize(new Dimension(120, 20));
        jComboBox1.setMinimumSize(new Dimension(120, 20));
        jComboBox1.setPreferredSize(new Dimension(120, 20));
	final Requests req1 = this.req;
        jButton1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
               // add new task
		try {
		 req1.newTask(memberIds[jComboBox1.getSelectedIndex()], jTextField1.getText());
		 f.dispose();
		 f.setVisible(false);
		} catch(Exception e) {
		    System.out.println(e);
		}
            }
        });
        jPanel1.add(jComboBox1);

        f.getContentPane().add(jPanel1);

        jPanel2.setLayout(new FlowLayout(FlowLayout.LEFT));

        jLabelTask.setHorizontalAlignment(SwingConstants.RIGHT);
        jLabelTask.setText("Task:");
        jLabelTask.setMaximumSize(new Dimension(50, 20));
        jLabelTask.setMinimumSize(new Dimension(50, 20));
        jLabelTask.setPreferredSize(new Dimension(50, 20));
        jPanel2.add(jLabelTask);

        jTextField1.setFont(new Font("Verdana", 0, 11));
        jTextField1.setMargin(new Insets(1, 3, 1, 1));
        jTextField1.setMaximumSize(new Dimension(300, 20));
        jTextField1.setMinimumSize(new Dimension(300, 20));
        jTextField1.setPreferredSize(new Dimension(300, 20));
        jPanel2.add(jTextField1);

        f.getContentPane().add(jPanel2);

        jPanel3.setLayout(new FlowLayout(FlowLayout.RIGHT));

        jButton1.setIcon(new ImageIcon("/xml-collab/client/src/gui/icons/tango_assign.png"));
        jButton1.setText("Assign");
        jButton1.setToolTipText("Assign task");
        jButton1.setIconTextGap(5);
        jPanel3.add(jButton1);

        f.getContentPane().add(jPanel3);
	f.setVisible(true);
    }


    public String[] getMembers() throws Exception{
	List<Member> members = this.req.getMembers();
	String[] memberArray = new String[members.size()];
	memberIds = new Integer[members.size()];
	
	for(Integer i=0; i< members.size(); i++) {
	    memberArray[i] = members.get(i).mName;
	    memberIds[i] = members.get(i).mId;
        }
	return memberArray;
    }

    public Integer getMemberId(String username)  throws Exception{
	List<Member> members = this.req.getMembers();

	for(Integer i=0; i< members.size(); i++) {
	    if(members.get(i).mName.equals(username)) {
		return members.get(i).mId;
	    }
        }
	return 0;
    }

}
